<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_thingsplaces extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize()
	{
		require('engine/class.www-database.php');
		$this->conn=new WWW_Database();
		require('engine/include_dao.php');
		return true;
		
	}
	public function create_thingsarticle($data)
	{	
		$thingsArticle = new ThingsArticle();
		$thingsArticle->article_name = $data['article_name'];
		$thingsArticle->created_date = date("Y-m-d h:i:s");
		$thingsArticle->updated_date = date("Y-m-d h:i:s");
		$result = DAOFactory::getThingsArticleDAO()->insert($thingsArticle);
		//print_r($result);exit;
	/* $query="insert into `things_article` (article_name,created_date,updated_date) VALUES ('".$data['article_name']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	$result=$this->conn->dbCommand($query);	 */
		//$this->db->last_query();exit;
	if($result !== 0){
			return array('message'=>'Article successfully created');			
		}else{
			return array('message'=>'Article Creation Failed');
		}	
	}
	public function create_thingstodo($data)
	{ 
		$thingsToDo = new ThingsToDo();
		$thingsToDo->article_id = $data['article_id'];
		$thingsToDo->things_place = $data['things_place'];
		$thingsToDo->things_location = $data['things_location'];
		$thingsToDo->description = $data['description'];
		$thingsToDo->created_date = date("Y-m-d h:i:s");
		$thingsToDo->updated_date = date("Y-m-d h:i:s");
		$result = DAOFactory::getThingsToDoDAO()->insert($thingsToDo);
		//print_r($result);exit;
		
		//echo "<pre>"; print_r($data);exit;
	/* $query="insert into `things_to_do` (article_id,things_place,things_location,description,created_date,updated_date) VALUES ('".$data['article_id']."','".$data['things_place']."','".$data['things_location']."','".$data['description']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')"; */
	
		if($result !== 0){
		//$lastId=$this->conn->dbLastId();
		$data['things_id'] = $result;
		//print_r($data['things_id']);exit;
		
		foreach($data["things_image"]['tmp_name'] as $id=>$filetmpname){
			
		$filename=time().$data["things_image"]['name'][$id];
		$target_file="uploads/things_to_do/".$filename;
		move_uploaded_file($filetmpname, $target_file);
		
		$thingsToDo = new ThingsToDo();
		$thingsToDo->things_id = $data['things_id'];
		$thingsToDo->things_image_title = $data["things_image"]['size'][$id];
		$thingsToDo->image_name = $data["things_image"]['name'][$id];
		$thingsToDo->things_image = $this->conn->baseurl().$target_file;
		$thingsToDo->created_date = date("Y-m-d h:i:s");
		$thingsToDo->updated_date = date("Y-m-d h:i:s");
		$result1 = DAOFactory::getThingsToDoDAO()->insert_image($thingsToDo);
		//print_r($result1);exit;
		}
		//return array('result'=>$target_file);exit;
		/* $query1 = "insert into things_images (things_id,things_image_title,image_name,things_image,created_date,updated_date) values('".$lastId."','".$data["things_image"]['name'][$id]."','".$data["things_image"]['title'][$id]."','".$this->conn->baseurl().$target_file."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
		//return array('result'=>$query1);exit;
		$result1=$this->conn->dbCommand($query1); */
		
		
	}
	if($result1 !== 0){
			return array('message'=>'things successfully created');			
		}else{
			return array('message'=>'things Creation Failed');
		}	
	}
	public function get_articles_list()
	{	
		$thingsArticle = new ThingsArticle();
		$result = DAOFactory::getThingsArticleDAO()->articles_list($thingsArticle);
		//print_r($result);exit;
		/* $query = "select article_name,article_id from things_article";
		$result = $this->conn->dbMultiple($query); */		
		if($result !== 0){
			return array('message'=>'Category type data','result'=>$result);		
		}else{
			return array('message'=>'Category by id is failed');
		}
	}
	public function edit_thingsarticle($data)
	{		
		$thingsArticle = new ThingsArticle();
		$thingsArticle->article_name = $data['article_name'];
		$thingsArticle->article_id = $data['article_id'];
		$thingsArticle->created_date = date("Y-m-d h:i:s");
		$thingsArticle->updated_date = date("Y-m-d h:i:s");
		$result = DAOFactory::getThingsArticleDAO()->update($thingsArticle);
		//print_r($result);exit;
	
	/* $query="update `things_article` set article_name='".$data['article_name']."',updated_date='".date("Y-m-d H:i:s")."' where article_id=".$data['article_id']; 
		$result=$this->conn->dbCommand($query);*/		
		if($result == 1){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	public function edit_thingstodo($data)
	{	 	
		$thingsToDo = new ThingsToDo();
		$thingsToDo->things_id = $data['things_id'];
		$thingsToDo->article_id = $data['article_id'];
		$thingsToDo->things_place = $data['things_place'];
		$thingsToDo->things_location = $data['things_location'];
		$thingsToDo->description = $data['description'];
		$thingsToDo->status = $data['status'];
		$thingsToDo->updated_date = date("Y-m-d h:i:s");
		if(DAOFactory::getThingsToDoDAO()->update($thingsToDo))
		
		
		/* echo "<pre>";print_r($data);exit;
		$query="update `things_to_do` set article_id='".$data['article_id']."',things_place='".$data['things_place']."',things_location='".$data['things_location']."',description='".$data['description']."',updated_date='".date("Y-m-d H:i:s")."' where things_id='".$data['things_id']."'";
		
			if($result=$this->conn->dbCommand($query)) */
			{	
			if($data['remove_id']!="")
			{	//echo "<pre>";print_r($data['image_id']);exit;
				foreach($data['remove_id'] as $id=>$remove_id)
				{	
				$thingsToDo = new ThingsToDo();
				$thingsToDo->image_id = $remove_ids[$id];
				$result = DAOFactory::getThingsToDoDAO()->remove($thingsArticle);
				/* $query1 = "delete from things_images where image_id='".$remove_ids[$id]."'";
					//print_r($query1);exit;
				$result = $this->conn->dbCommand($query1); */
			}
			} 
			if($data["things_image"]['tmp_name']!="")
			{	
				$images = explode(',', $data['image_id']); 
				$new_names = explode(',', $data['new_name']); 
				
				
				
				foreach($data['things_image']['name'] as $id=>$image_name)
				{		
				
				if(in_array($image_name, $new_names))
				{
				$file = $data["things_image"]['tmp_name'][$id];
				$filename=time().$image_name;
				$target_file="uploads/things_to_do/".$filename;
				move_uploaded_file($file, $target_file);
				//print_r($data["things_image"]['name'][$id]);exit;
				$thingsToDo = new ThingsToDo();
				$thingsToDo->things_id = $data['things_id'];
				$thingsToDo->image_id = $data['image_id'];
				$thingsToDo->things_image_title = $data["things_image"]['title'][$id];
				$thingsToDo->image_name = $data["things_image"]['name'][$id];
				$thingsToDo->things_image = $this->conn->baseurl().$target_file;
				$thingsToDo->updated_date = date("Y-m-d h:i:s");
				$result = DAOFactory::getThingsToDoDAO()->update_image($thingsToDo);
				//print_r($result);exit;
				/* $query = "update things_images set things_id='".$data['things_id']."',things_image_title='".$data["things_image"]['name'][$id]."',image_name='".$data["things_image"]['size'][$id]."',things_image='".$this->conn->baseurl().$target_file."' where image_id='".$images[$id]."'";
				//print_r($query);exit;
				$result=$this->conn->dbCommand($query); */
				}
				else 
				{
				$filename=time().$image_name;
				//echo "<pre>";print_r($filename);
				$file = $data["things_image"]['tmp_name'][$id];
				$target_file="uploads/things_to_do/".$filename;
				move_uploaded_file($file, $target_file);
				$thingsToDo = new ThingsToDo();
				$thingsToDo->things_id = $data['things_id'];
				$thingsToDo->things_image_title = $data["things_image"]['title'][$id];
				$thingsToDo->image_name = $data["things_image"]['name'][$id];
				$thingsToDo->things_image = $this->conn->baseurl().$target_file;
				$thingsToDo->updated_date = date("Y-m-d h:i:s");
				$result = DAOFactory::getThingsToDoDAO()->insert_image($thingsToDo);
				//print_r($result);exit;
				/* $query1 = "insert into things_images (things_id,things_image_title,image_name,things_image,created_date,updated_date) values('".$data['things_id']."','".$data["things_image"]['name'][$id]."','".$data["things_image"]['size'][$id]."','".$this->conn->baseurl().$target_file."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')"; 
				//print_r($query1);exit;
				$result=$this->conn->dbCommand($query1);*/
				}
			}
			
			
			}
			//echo "<pre>";print_r($data);exit;				
				}
				if($result !==""){
						return array('message'=>'Updated Successfully');			
					}else{
						return array('message'=>'Updates Failed');
					}	
		
	}
	public function get_thingsarticle($data)
	{
		$thingsArticle = new ThingsArticle();
		$thingsArticle->article_id = $data['article_id'];
		$result = DAOFactory::getThingsArticleDAO()->load($thingsArticle);
		//print_r($result);exit;
		/* $query="SELECT * from things_article
 WHERE article_id='".$data['article_id']."'";
		
	//print_r($result->things_image);exit;
		$result=$this->conn->dbMultiple($query);	 */	
		
		//print_r($result->things_image);exit;
		if($result!==""){
			return array('message'=>'Article type data','result'=>$result);		
		}else{
			return array('message'=>'Article by id is failed');
		}	
	
	}
	public function get_thingstodo($data)
	{
		$thingsToDo = new ThingsToDo();
		$thingsToDo->things_id = $data['things_id'];
		$result = DAOFactory::getThingsToDoDAO()->load($thingsToDo);
		//$result1 = DAOFactory::getThingsToDoDAO()->thingstodo_images($thingsArticle);
		//print_r($result);exit;
	/* $query="SELECT things_to_do.* ,tc.article_name FROM things_to_do  left join things_article tc on things_to_do.article_id=tc.article_id  where things_id='".$data['things_id']."'";
		$result=$this->conn->dbMultiple($query); */		
		
		if($result){
			return array('message'=>'Things type data','result'=>$result);			
		}else{
			return array('message'=>'Things by id is failed');
		}	
	
	}
	public function get_things_images($data)
	{	
		
		$thingsArticle = new ThingsArticle();
		$thingsArticle->things_id = $data['things_id'];
		$result = DAOFactory::getThingsToDoDAO()->thingstodo_images($thingsArticle);
		/* $query = "select * from things_images where things_id='".$data['things_id']."'";
		$result=$this->conn->dbMultiple($query); */		
		
		if($result){
			return array('message'=>'Things type data','result'=>$result);			
		}else{
			return array('message'=>'Things by id is failed');
		}
	}
	public function delete_thingsarticle($data)
	{	
		//print_r(json_decode($data['location_id']));exit;
		
		$thingsArticle = new ThingsArticle();
		$thingsArticle->article_id = $data['article_id'];
		$thingsArticle->status = $data['status'];
		$result = DAOFactory::getThingsArticleDAO()->delete($thingsArticle);
		//print_r($result);exit;
		/* $query="update `things_article` set status='".$data['status']."' where article_id='".$data['article_id']."'";
		$data=$this->conn->dbCommand($query); */
		if($result == 1)
			return array('message'=>"Article status changed Successfully");		
		else
			return array('message'=>'Article status changed failed');
	}
	public function delete_thingstodo($data)
	{		
		$thingsToDo = new ThingsToDo();
		$thingsToDo->things_id = $data['things_id'];
		$thingsToDo->status = $data['status'];
		$result = DAOFactory::getThingsToDoDAO()->delete($thingsToDo);
		//print_r($result);exit;
		$query="update `things_to_do` set status='".$data['status']."' where things_id='".$data['things_id']."'";
		$data=$this->conn->dbCommand($query);
		if($result!="")
			return array('message'=>"Things Deleted Successfully");		
		else
			return array('message'=>'Things Deletion Fails');
	}
	
	public function all_thingsarticle($data){
		
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
		$thingsArticle = new ThingsArticle();
		$thingsArticle->results_per_page = $results_per_page;
		$thingsArticle->order_by = $order_by;
		$thingsArticle->sort_order = $sort_order;
		$thingsArticle->offset = $offset;
		$result = DAOFactory::getThingsArticleDAO()->queryAll_article($thingsArticle);
		$totalrows = DAOFactory::getThingsArticleDAO()->article_count($thingsArticle);
		//print_r($totalrows);exit;
		
		/* $query="select * from things_article order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$result=$this->conn->dbMultiple($query);
		$query1 = "SELECT count(*) as total from things_article"; 
		
		$result=$this->conn->dbMultiple($query);*/
		if($result!==""){
			//$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All ","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No ThingsPace Articles');
		
	}
	
	public function all_thingstodo($data)
	{	
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	
	
	
	/* $query_string="SELECT things_to_do.*,tc.article_name  FROM things_to_do  left join things_article tc on things_to_do.article_id=tc.article_id";
	$query_string1="select count(*) as total
					FROM things_to_do  left join things_article tc on things_to_do.article_id=tc.article_id"; */
	if (isset($data['search_by']) && $data['search_by']!='')
	{	//print_r($data);exit;
		
		if($data['search_by'] == 'things_location')
		{
			$search_by = "things_location";
		}
		if($data['search_by'] == 'things_place')
		{
			$search_by = "things_place";
		}
		if($data['search_by'] == 'article_name')
		{
			$search_by = "tc.article_name";
		}
		$thingsToDo = new ThingsToDo();
		$thingsToDo->results_per_page = $results_per_page;
		$thingsToDo->order_by = $order_by;
		$thingsToDo->sort_order = $sort_order;
		$thingsToDo->offset = $offset;
		$thingsToDo->search_by = $search_by;
		$thingsToDo->search_by_value = $data['search_by_value'];
		$result = DAOFactory::getThingsToDoDAO()->searchby_thingstodo($thingsToDo);
		$totalrows = DAOFactory::getThingsToDoDAO()->searchbycount($thingsToDo);
		//print_r($totalrows);exit;
		
		 /* $query = $query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 =$query_string1." where ".$search_by." like '%".$data['search_by_value']."%'"; */ 
		
	}else
	{
		$thingsToDo = new ThingsToDo();
		$thingsToDo->results_per_page = $results_per_page;
		$thingsToDo->order_by = $order_by;
		$thingsToDo->sort_order = $sort_order;
		$thingsToDo->offset = $offset;
		$result = DAOFactory::getThingsToDoDAO()->queryAll_thingstodo($thingsToDo);
		$totalrows = DAOFactory::getThingsToDoDAO()->thingstodo_count($thingsToDo);
		//print_r($result);exit; 
	 $query=$query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset; 
	
	//$query1 = $query_string1;
	}	//print_r($query);exit;
		//$result=$this->conn->dbMultiple($query);
		if($result !== ''){
			//$totalrows = $this->conn->dbSingle($query1); 
		return array('message'=>"All ","info"=>$result,'total'=>$totalrows,'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No ThingsPaces');
		
	}
}
	
?>